

// Cookie 操作者
export default {
    /**
     * 设置
     * @param obj - 存储实体
     * @param exMinutes - 失效分钟
     * @param exHours - 失效小时
     * @param exDays - 失效天数
     * */
    set(obj, exMinutes = 0, exHours = 0, exDays = 0) {

        // 一分钟转换毫秒
        let oneMinuteMs = 60 * 1000;
        // 一小时转换毫秒
        let oneHourMs = 60 * oneMinuteMs;
        // 一天转换毫秒
        let oneDayMs = 24 * oneMinuteMs;

        // 获取当前时间,累加时间
        let dateNow = new Date();
        let exDaysMs = exDays * oneDayMs;
        let exHourMs = exHours * oneHourMs;
        let exMinuteMs = exMinutes * oneMinuteMs;
        let exPlus = dateNow.getTime() + exDaysMs + exHourMs + exMinuteMs;

        // 设置时效
        if (exPlus > 0)
            obj.expires = `expires=${dateNow.setTime(exPlus).toGMTString()}`

        // 获取存储字符串
        let entries = Object.entries(obj);
        let entriesArry = entries.map(item => `${item[0]}=${item[1]}`);

        // 设置 Cookie
        document.cookie = entriesArry.join(";");
    },
    get() {
        let result = {};
        let cookieArray = document.cookie.split(';');

        // 遍历设置对象
        cookieArray.forEach((item) => {
            if (item.indexOf('expires=') == -1) {
                let itemArray = item.split('=');
                result[itemArray[0]] = itemArray[1];
            }
        })

        return result;
    }
}